<template>
  <div class="wrap">
    <div class="login">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button type="primary" @click="registerForm">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {}
    };
  },
  created() {},
  methods: {
    //登录
    submitForm() {
      axios({
        url: "/api/login",
        method: "post",
        data: this.ruleForm
      }).then(res => {
        if (res.data.code===0) {
          window.localStorage.setItem("token", res.data.token);
          this.$router.push("/");
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "success"
          });
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error"
          });
        }
      });
    },
    registerForm() {
      axios({
        url: "/api/register",
        method: "post",
        data: this.ruleForm
      }).then(res => {
        if (res.data.code) {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "success"
          });
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error"
          });
        }
      });
    }
  }
};
</script>

<style lang='scss' scoped>
.wrap {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-image: url("../img/美女.png");
}
.login {
  margin: 30%;
  width: 400px;
  height: 300px;
}
</style>